package com.springdoor.web.controller.goods;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.springdoor.common.core.domain.entity.SysDept;
import com.springdoor.system.service.ISysDeptService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.springdoor.common.annotation.Log;
import com.springdoor.common.core.controller.BaseController;
import com.springdoor.common.core.domain.AjaxResult;
import com.springdoor.common.enums.BusinessType;
import com.springdoor.goods.domain.SprdGoodsShop;
import com.springdoor.goods.service.ISprdGoodsShopService;
import com.springdoor.common.utils.poi.ExcelUtil;
import com.springdoor.common.core.page.TableDataInfo;

/**
 * 店铺商品关联Controller
 *
 * @author llshs
 * @date 2024-12-04
 */
@RestController
@RequestMapping("/goods/shop")
public class SprdGoodsShopController extends BaseController {

    @Autowired
    private ISprdGoodsShopService sprdGoodsShopService;

    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询店铺商品关联列表
     */
    @PreAuthorize("@ss.hasPermi('goods:shop:list')")
    @GetMapping("/list")
    public TableDataInfo list(SprdGoodsShop sprdGoodsShop) {
        startPage();
        List<SprdGoodsShop> list = sprdGoodsShopService.selectSprdGoodsShopList(sprdGoodsShop);
        return getDataTable(list);
    }

    /**
     * 查询店铺商品关联列表
     */
    @PreAuthorize("@ss.hasPermi('goods:shop:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll(SprdGoodsShop sprdGoodsShop) {
        sprdGoodsShop.setStatus("1");
        return success(sprdGoodsShopService.selectSprdGoodsShopList(sprdGoodsShop));
    }

    /**
     * 导出店铺商品关联列表
     */
    @PreAuthorize("@ss.hasPermi('goods:shop:export')")
    @Log(title = "店铺商品关联->导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SprdGoodsShop sprdGoodsShop)
    {
        List<SprdGoodsShop> list = sprdGoodsShopService.selectSprdGoodsShopList(sprdGoodsShop);
        ExcelUtil<SprdGoodsShop> util = new ExcelUtil<SprdGoodsShop>(SprdGoodsShop.class);
        util.exportExcel(response, list, "店铺商品关联数据");
    }

    /**
     * 获取店铺商品关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('goods:shop:query')")
    @GetMapping(value = "/{gshopId}")
    public AjaxResult getInfo(@PathVariable("gshopId") Long gshopId)
    {
        return success(sprdGoodsShopService.selectSprdGoodsShopByGshopId(gshopId));
    }

    /**
     * 新增店铺商品关联
     */
    @PreAuthorize("@ss.hasPermi('goods:shop:add')")
    @Log(title = "店铺商品关联->新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SprdGoodsShop sprdGoodsShop)
    {
        sprdGoodsShop.setCreateBy(getUsername());
        sprdGoodsShop.setUpdateBy(getUsername());
        return toAjax(sprdGoodsShopService.insertSprdGoodsShop(sprdGoodsShop));
    }

    /**
     * 修改店铺商品关联
     */
    @PreAuthorize("@ss.hasPermi('goods:shop:edit')")
    @Log(title = "店铺商品关联->修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SprdGoodsShop sprdGoodsShop)
    {
        sprdGoodsShop.setUpdateBy(getUsername());
        return toAjax(sprdGoodsShopService.updateSprdGoodsShop(sprdGoodsShop));
    }

    /**
     * 删除店铺商品关联
     */
    @PreAuthorize("@ss.hasPermi('goods:shop:remove')")
    @Log(title = "店铺商品关联->删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{gshopIds}")
    public AjaxResult remove(@PathVariable Long[] gshopIds)
    {
        return toAjax(sprdGoodsShopService.deleteSprdGoodsShopByGshopIds(gshopIds));
    }

    /**
     * 获取部门树列表
     */
    @PreAuthorize("@ss.hasPermi('goods:shop:list')")
    @GetMapping("/shopOptions")
    public AjaxResult shopOptions() {
        SysDept dept = new SysDept();
        dept.setIsShop("Y");
        return success(deptService.selectDeptList(dept));
    }

    /**
     * 获取部门树列表
     */
    @PreAuthorize("@ss.hasPermi('goods:shop:list')")
    @GetMapping("/shopTree")
    public AjaxResult shopTree() {
        SysDept dept = new SysDept();
        dept.setIsShop("Y");
        return success(deptService.selectShopTreeList(dept));
    }

    @PreAuthorize("@ss.hasPermi('goods:shop:edit')")
    @GetMapping("/findMaxOrderNum/{deptId}")
    public AjaxResult findMaxOrderNum(@PathVariable Long deptId) {
        return success(sprdGoodsShopService.findMaxOrderNum(deptId));
    }

    @GetMapping("/goodsTypeNumEcharts")
    public AjaxResult selectShopGoodsTypeNumEcharts() {
        return success(sprdGoodsShopService.getShopGoodsTypeNum());
    }

    /**
     * 一键上架店铺商品
     */
    @PreAuthorize("@ss.hasPermi('goods:shop:edit')")
    @Log(title = "店铺商品关联->一键上架", businessType = BusinessType.UPDATE)
    @PostMapping("/putOnShops")
    public AjaxResult putOnShops(@RequestBody Long[] gshopIds)
    {
        if (gshopIds == null || gshopIds.length == 0) {
            return toAjax(0);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("updateBy", getUsername());
        map.put("status", "1");
        map.put("gshopIds", gshopIds);
        return toAjax(sprdGoodsShopService.putOnShops(map));
    }

    /**
     * 一键下架店铺商品
     */
    @PreAuthorize("@ss.hasPermi('goods:shop:edit')")
    @Log(title = "店铺商品关联->一键下架", businessType = BusinessType.UPDATE)
    @PostMapping("/putOutShops")
    public AjaxResult putOutShops(@RequestBody Long[] gshopIds)
    {
        if (gshopIds == null || gshopIds.length == 0) {
            return toAjax(0);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("updateBy", getUsername());
        map.put("status", "9");
        map.put("gshopIds", gshopIds);
        return toAjax(sprdGoodsShopService.putOnShops(map));
    }

    /**
     * 获取店铺商品关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('goods:shop:query')")
    @GetMapping(value = "/getGoodsMain/{deptId}/{gNumber}")
    public AjaxResult getGoodsMain(@PathVariable("deptId") Long deptId, @PathVariable("gNumber") String gNumber)
    {
        SprdGoodsShop sprdGoodsShop = new SprdGoodsShop();
        sprdGoodsShop.setDeptId(deptId);
        sprdGoodsShop.setgNumber(gNumber);
        return success(sprdGoodsShopService.selectGoodsMainByDeptGnumber(sprdGoodsShop));
    }
}
